package simpleGeom;

import java.util.Vector;

public class WingedEdge {
	WingedEdge leftNext;
	WingedEdge leftPrev;
	WingedEdge rightNext;
	WingedEdge rightPrev;
	WingedNode n1;
	WingedNode n2;
	WingedFace leftFace;
	WingedFace rightFace;

	public WingedEdge(WingedNode n1, WingedNode n2) {
		this.n1 = n1;
		this.n2 = n2;
	}

	public void attach() {

	}

	public WingedNode getOther(WingedNode n) {
		if (n == n1)
			return n2;
		return n1;
	}

	public WingedFace getOther(WingedFace f) {
		if (f == leftFace)
			return rightFace;
		return leftFace;
	}

	Vector<WingedNode> getRightNodes() {
		Vector<WingedNode> nodes = new Vector<WingedNode>();
		WingedNode startNode = n2;
		WingedNode lastNode = startNode;
		WingedEdge nextEdge = this;
		do {
			if (nextEdge.n1 == lastNode) {
				nodes.add(nextEdge.n1);
				lastNode = nextEdge.n1;
				nextEdge = nextEdge.leftNext;
			} else {
				nodes.add(nextEdge.n2);
				lastNode = nextEdge.n2;
				nextEdge = nextEdge.rightPrev;
			}
		} while (lastNode != startNode);
		return nodes;
	}

	Vector<WingedNode> getLeftNodes() {
		Vector<WingedNode> nodes = new Vector<WingedNode>();
		WingedNode startNode = n1;
		WingedNode lastNode = startNode;
		WingedEdge nextEdge = this;
		do {
			if (nextEdge.n1 == lastNode) {
				nodes.add(nextEdge.n1);
				lastNode = nextEdge.n1;
				nextEdge = nextEdge.leftNext;
			} else {
				nodes.add(nextEdge.n2);
				lastNode = nextEdge.n2;
				nextEdge = nextEdge.rightPrev;
			}
		} while (lastNode != startNode);
		return nodes;
	}

	Vector<WingedEdge> getLeftEdges() {
		Vector<WingedEdge> edges = new Vector<WingedEdge>();
		WingedNode startNode = n1;
		WingedNode lastNode = startNode;
		WingedEdge nextEdge = this;
		do {
			if (nextEdge.n1 == lastNode) {
				edges.add(nextEdge);
				lastNode = nextEdge.n1;
				nextEdge = nextEdge.leftNext;
			} else {
				edges.add(nextEdge);
				lastNode = nextEdge.n2;
				nextEdge = nextEdge.rightPrev;
			}
		} while (lastNode != startNode);
		return edges;
	}

	Vector<WingedEdge> getRightEdges() {
		Vector<WingedEdge> edges = new Vector<WingedEdge>();
		WingedNode startNode = n2;
		WingedNode lastNode = startNode;
		WingedEdge nextEdge = this;
		do {
			if (nextEdge.n1 == lastNode) {
				edges.add(nextEdge);
				lastNode = nextEdge.n1;
				nextEdge = nextEdge.leftNext;
			} else {
				edges.add(nextEdge);
				lastNode = nextEdge.n2;
				nextEdge = nextEdge.rightPrev;
			}
		} while (lastNode != startNode);
		return edges;
	}

	public Line getLine2D() {
		return new Line(n1.x, n1.y, n2.x, n2.y);
	}

}
